package com.cxlin.cwanandroid.ui.systemtype.viewmodel

import androidx.lifecycle.viewModelScope
import com.cxlin.base.BaseViewModel
import com.cxlin.base.model.api.Result
import com.cxlin.base.mvi.UiEvent
import com.cxlin.base.mvi.UiIntent
import com.cxlin.base.mvi.UiState
import com.cxlin.cwanandroid.model.bean.ArticleList
import com.cxlin.cwanandroid.model.repository.CollectRepository
import com.cxlin.cwanandroid.model.repository.SystemRepository
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

/**
 *
 * @Author: chenxunlin
 * @Date: 2024/4/30 17:37
 */
class SystemTypeViewModel(
    val systemRepository: SystemRepository,
    val collectRepository: CollectRepository
) : BaseViewModel() {

    sealed class SystemTypeUiState : UiState {
        object Idel : SystemTypeUiState()

        data class IsRefresh(val result : Boolean) : SystemTypeUiState()

        data class Articles(val result: ArticleList?) : SystemTypeUiState()
    }

    sealed class SystemTypeUiEvent : UiEvent {
        class ToastEvent(val message: String, val short: Boolean = false) : SystemTypeUiEvent()
    }

    sealed class SystemTypeUiIntent : UiIntent {
        data class GetArticleList(val cid: Int, val isRefresh: Boolean = false) :
            SystemTypeUiIntent()

        data class CollectArticle(val articleId: Int, val boolean: Boolean) :
            SystemTypeUiIntent()
    }

    private var currentPage = 0

    override fun handleIntent(intent: UiIntent) {
        when (intent) {
            is SystemTypeUiIntent.GetArticleList -> {
                viewModelScope.launch {
                    kotlin.runCatching {
                        if (intent.isRefresh) {
                            currentPage = 0
                            mUiStateFlow.update { SystemTypeUiState.IsRefresh(true) }
                        }
                        val result = systemRepository.getSystemTypeDetail(intent.cid,currentPage)
                        if (result is Result.Success) {
                            mUiStateFlow.update { SystemTypeUiState.IsRefresh(false) }
                            mUiStateFlow.update { SystemTypeUiState.Articles(result.data) }
                            currentPage += 1
                        } else if (result is Result.Error) {
                            mUiStateFlow.update { SystemTypeUiState.IsRefresh(false) }
                            mEffectFlow.emit(SystemTypeUiEvent.ToastEvent(result.exception.message ?: "网络异常", true))
                        }
                    }
                }
            }

            is SystemTypeUiIntent.CollectArticle -> {
                viewModelScope.launch {
                    kotlin.runCatching {
                        if (intent.boolean) {
                            collectRepository.collectArticle(intent.articleId)
                        } else {
                            collectRepository.unCollectArticle(intent.articleId)
                        }

                    }
                }
            }
        }
    }

    override fun createInitState(): UiState {
        return SystemTypeUiState.Idel
    }
}